package com.bwie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.BrandMapper;
import com.bwie.pojo.Brand;
import com.bwie.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService{
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private HttpServletRequest request;
    @Override
    public R getBrandList(PageInfoVo pageInfoVo) {
        Page<Brand> brandPage = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        brandQueryWrapper.lambda().orderByDesc(Brand::getCreateTime);
        Page<Brand> brandPage1 = brandMapper.selectPage(brandPage, brandQueryWrapper);
        return R.OK(brandPage1);
    }

    @Override
    public R addBrand(Brand brand) {
        Brand brand1 = brandMapper.selectOne(
                new QueryWrapper<Brand>().lambda().eq(Brand::getBrandName, brand.getBrandName())
        );
        if(ObjectUtils.isNotEmpty(brand1)){
            return R.ERROR(500,"品牌名称存在");
        }
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return R.ERROR(500,"请先登录");
        }
        String userName = TokenUtils.getUserName(token);
        brand.setCreateBy(userName);
        brand.setCreateTime(new Date());
        brandMapper.insert(brand);
        return R.OK();
    }

    @Override
    public R updateBrandStatus(Brand brand) {
        Brand brand1 = brandMapper.selectById(brand.getBrandId());
        if(ObjectUtils.isEmpty(brand1)){
            return R.ERROR(500,"品牌不存在");
        }
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return R.ERROR(500,"请先登录");
        }
        String userName = TokenUtils.getUserName(token);
        brand1.setUpdateBy(userName);
        brand1.setUpdateTime(new Date());
        brand1.setBrandStatus(brand.getBrandStatus());
        brandMapper.updateById(brand);
        return R.OK();
    }

    @Override
    public R dateBrand(Integer brandId) {
        Brand brand = brandMapper.selectOne(
                new QueryWrapper<Brand>().lambda().eq(Brand::getBrandId, brandId)
        );
        if(ObjectUtils.isEmpty(brand)){
            return R.ERROR(500,"数据不存在");
        }
        brandMapper.deleteById(brandId);
        return R.OK();
    }

    @Override
    public R deteBrandIds(List<Brand> brandList) {
        List<Integer> collect = brandList.stream().map(item -> item.getBrandId()).collect(Collectors.toList());
        brandMapper.deleteBatchIds(collect);
        return R.OK();
    }
}